package com.lebang.service;

import com.lebang.domain.LogoutRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.example.LogoutRecordExample;
import com.lebang.mapper.*;
import com.lebang.model.ResultModel;
import com.lebang.model.enums.buss.TaskStatus;
import com.lebang.model.enums.buss.UserStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xwj
 * @Date: 2020/06/17 13:44
 * @Version 1.0
 */
@Service
public class LogoutService {

    @Autowired
    private LbbMemberRecordMapper lbbMemberRecordMapper;
    @Autowired
    private MemberCashWithdrawalMapper memberCashWithdrawalMapper;
    @Autowired
    private LogoutRecordMapper logoutRecordMapper;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    @Autowired
    private LbbMemberTradeMapper lbbMemberTradeMapper;
    @Autowired
    private LbbMemberNoticeMapper lbbMemberNoticeMapper;
    @Autowired
    private MemberPointInfoMapper memberPointInfoMapper;
    @Autowired
    private MemberPointRecordMapper memberPointRecordMapper;


    public List<ResultModel> checkAcount(LbbMember member, RequestHeaders requsetHeader) {
        List<ResultModel> list = new ArrayList<>();

        //1.验证设备号是否一致
        if (!requsetHeader.getImei().equals(member.getImei())) {
            list.add(new ResultModel("检测到账号存在异常", "当前设备与注册设备不一致"));
        }
        //2.验证订单审核状态,提现申请
       // int taskCount = lbbMemberRecordMapper.countByQuery(requsetHeader.getUserid(),requsetHeader.getAppid(), TaskStatus.WaitCheckout.name());
        int taskCount = lbbMemberRecordMapper.countByQuery1(requsetHeader.getUserid(),requsetHeader.getAppid());

        int cashCount = memberCashWithdrawalMapper.countByStatus(requsetHeader.getUserid());
        if (taskCount>0||cashCount>0) {
            list.add(new ResultModel("订单未结算清", "当前有待审核任务或提现申请等记录"));
        }

        return list;
    }

    public LogoutRecord insertRecord(LbbMember member, RequestHeaders requsetHeader,long money,long allMoney,String msg,String pictureOne,String pictureTwo) {
        LocalDateTime now = LocalDateTime.now();
        LogoutRecord logoutRecord = new LogoutRecord();
        logoutRecord.setCreateTime(now);
        logoutRecord.setModifyTime(now);
        logoutRecord.setMemberId(member.getMemberId());
        logoutRecord.setImei(requsetHeader.getImei());
        logoutRecord.setPhone(member.getPhone());
        logoutRecord.setMoney(money);
        logoutRecord.setAllMoney(allMoney);
        logoutRecord.setMsg(msg);
        logoutRecord.setCardPictureOne(pictureOne);
        logoutRecord.setCardPrctureTwo(pictureTwo);
        logoutRecord.setStatus(0);
        logoutRecord.setBrand(requsetHeader.getBrand());
        logoutRecord.setIp(requsetHeader.getIpAddr());
        logoutRecordMapper.insertSelective(logoutRecord);
        member.setStatus(UserStatus.WaitCheck.name());
        lbbMemberMapper.updateByPrimaryKeySelective(member);
        return logoutRecord;
    }

    public LogoutRecord getRecord(Integer id) {
     return logoutRecordMapper.selectByPrimaryKey(id);
    }

    /**
     * 找回
     */
    public void backOpera(RequestHeaders requsetHeader, LbbMember member, LogoutRecord record) {

        member.setStatus(UserStatus.Allow.name());
        lbbMemberMapper.updateByPrimaryKeySelective(member);
        //用户撤回状态
        record.setStatus(3);
        record.setIsFinish(1);
        logoutRecordMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 注销
     */
    public void sureOpera(RequestHeaders requsetHeader, LbbMember member, LogoutRecord record) {
            //用户状态置为已注销
            member.setStatus(UserStatus.CheckSuccess.name());
            member.setAvatar("");
            member.setAlipay("");
            member.setAlipayName("");
            member.setUnionid("");
            member.setWeixin("");
            member.setWeixinName("");
            lbbMemberMapper.updateByPrimaryKeySelective(member);

            //注销审核记录置为用户注销状态
            record.setStatus(4);
            record.setIsFinish(1);
            logoutRecordMapper.updateByPrimaryKeySelective(record);

            //用户账号的所有记录做逻辑删除
            lbbMemberTradeMapper.updateByMemberId(member.getMemberId());
            lbbMemberNoticeMapper.updateByMemberId(member.getMemberId());
            lbbMemberRecordMapper.updateByMemberId(member.getMemberId());
            memberCashWithdrawalMapper.updateByMemberId(member.getMemberId());
            memberPointRecordMapper.updateByMemberId(member.getMemberId());
           // memberWalletMapper.logoutMoney(member.getMemberId());
            memberPointInfoMapper.logoutPoint(member.getMemberId());


    }


    public boolean checkCount(LbbMember member, RequestHeaders requsetHeader) {
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now.getYear(),now.getMonth(),1,0,0,0);
        LocalDateTime endd = start.plusMonths(1).plusDays(-1);
        LocalDateTime end = LocalDateTime.of(endd.getYear(),endd.getMonth(),endd.getDayOfMonth(),23,59,59);
        //3.验证申请注销次数
        LogoutRecordExample example = new LogoutRecordExample();
        example.createCriteria().andPhoneEqualTo(member.getPhone()).andStatusEqualTo(4).andCreateTimeBetween(start,end);
        long phoneCount = logoutRecordMapper.countByExample(example);
        LogoutRecordExample example1 = new LogoutRecordExample();
        example1.createCriteria().andImeiEqualTo(requsetHeader.getImei()).andStatusEqualTo(4).andCreateTimeBetween(start,end);
        long imeiCount = logoutRecordMapper.countByExample(example1);

        if(phoneCount>3||imeiCount>3){
            return true;
        }
        return false;
    }

    public void refuseAll(LbbMember member, RequestHeaders requsetHeader) {
        lbbMemberRecordMapper.checkMemberAllRecord(member.getMemberId(),requsetHeader.getAppid(),TaskStatus.CheckFailure.name());
        memberCashWithdrawalMapper.checkMemberAllRecord(member.getMemberId());

    }

    public LogoutRecord selectRecord(String memberId,Integer status) {
       return logoutRecordMapper.selectOneByMemberId(memberId,status);
    }
}
